<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>主持人端 - 抢答器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            height: 100vh;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            width: 100%;
            max-width: 500px;
            text-align: center;
        }
        h1 {
            margin-bottom: 30px;
            font-size: 2.2rem;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }
        .status-panel {
            background: rgba(255, 255, 255, 0.1);
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            backdrop-filter: blur(10px);
        }
        .status-text {
            font-size: 1.4rem;
            margin-bottom: 15px;
        }
        .winner-display {
            font-size: 1.8rem;
            font-weight: bold;
            margin: 20px 0;
            min-height: 40px;
        }
        .round-info {
            font-size: 1.2rem;
            opacity: 0.8;
            margin-top: 10px;
        }
        .btn {
            background: #FFD700;
            color: #333;
            border: none;
            border-radius: 50px;
            padding: 15px 40px;
            font-size: 1.2rem;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
            margin: 10px;
        }
        .btn:disabled {
            background: #cccccc;
            color: #666;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        .btn:not(:disabled):hover {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
        }
        .btn:not(:disabled):active {
            transform: translateY(1px);
        }
        .reset-btn {
            background: #ff6b6b;
            color: white;
        }
        .connection-status {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.8rem;
        }
        .connected {
            background: rgba(76, 175, 80, 0.8);
        }
        .disconnected {
            background: rgba(244, 67, 54, 0.8);
        }
    </style>
</head>
<body>
<div class="connection-status disconnected" id="connectionStatus">未连接</div>
<audio id="startSound" src="/uploads/20251107/10d8ee02daecc7518a0a028e6c7f96e6.mp3" preload="auto"></audio>

<!-- 添加到 <body> 标签内，靠近其他元素的位置 -->
<audio id="answerSound" src="/uploads/20251107/1eab509df800f380b2590e992d43ee9a.mp3" preload="auto"></audio>

<div class="container">
    <h1>主持人端</h1>

    <div class="status-panel">
        <div class="status-text" id="statusText">等待开始</div>
        <div class="winner-display" id="winnerDisplay"></div>
        <div class="round-info">第 <span id="roundNumber">1</span> 轮</div>
    </div>

    <button class="btn" id="startBtn" disabled>开始抢答</button>
    <button class="btn reset-btn" id="resetBtn">重置</button>
</div>

<script>
    class HostClient {
        constructor() {
            this.ws = null;
            this.connected = false;
            this.status = 0; // 0:未开始 1:抢答中 2:已结束
            this.winnerId = null;
            this.round = 1;

            this.init();
        }

        init() {
            this.connect();
            this.bindEvents();
        }

        connect() {
            const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            const wsUrl = `${protocol}//${window.location.hostname}/wss`;

            this.ws = new WebSocket(wsUrl);

            this.ws.onopen = () => {
                console.log('WebSocket连接已建立');
                this.connected = true;
                this.updateConnectionStatus();
                this.loginAsHost();
            };

            this.ws.onmessage = (event) => {
                const data = JSON.parse(event.data);
                this.handleMessage(data);
            };

            this.ws.onclose = () => {
                console.log('WebSocket连接已关闭');
                this.connected = false;
                this.updateConnectionStatus();

                // 5秒后尝试重连
                setTimeout(() => {
                    this.connect();
                }, 5000);
            };

            this.ws.onerror = (error) => {
                console.error('WebSocket错误:', error);
                this.connected = false;
                this.updateConnectionStatus();
            };
        }

        bindEvents() {
            document.getElementById('startBtn').addEventListener('click', () => {
                this.startAnswer();
            });

            document.getElementById('resetBtn').addEventListener('click', () => {
                this.resetGame();
            });
        }

        loginAsHost() {
            this.sendMessage({
                type: 'host_login'
            });
        }

        startAnswer() {
            // 播放音效
            const sound = document.getElementById('startSound');
            if (sound) {
                sound.currentTime = 0; // 重置音频到开头
                sound.play().catch(e => console.log('音频播放失败:', e));
            }


            this.sendMessage({
                type: 'start_answer'
            });
        }

        resetGame() {
            if (confirm('确定要重置抢答吗？这将开始新的一轮。')) {
                this.sendMessage({
                    type: 'reset'
                });
            }
        }

        sendMessage(message) {
            if (this.connected && this.ws.readyState === WebSocket.OPEN) {
                this.ws.send(JSON.stringify(message));
            }
        }

        handleMessage(data) {
            switch (data.type) {
                case 'status_update':
                    this.status = data.status;
                    this.winnerId = data.winner_id;
                    this.round = data.round;
                    this.updateUI();
                    break;
                case 'game_started':
                    this.status = 1;
                    this.winnerId = null;
                    this.updateUI();
                    break;
                case 'game_result':
                    this.status = 2;
                    this.winnerId = data.winner_id;
                    this.updateUI();
                    break;
                case 'game_reset':
                    this.status = 0;
                    this.winnerId = null;
                    this.round = data.round || this.round + 1;
                    this.updateUI();
                    break;
            }
        }

        updateUI() {
            const statusText = document.getElementById('statusText');
            const winnerDisplay = document.getElementById('winnerDisplay');
            const roundNumber = document.getElementById('roundNumber');
            const startBtn = document.getElementById('startBtn');
            const resetBtn = document.getElementById('resetBtn');

            roundNumber.textContent = this.round;

            // 重置按钮始终保持可用
            resetBtn.disabled = false;

            switch (this.status) {
                case 0:
                    statusText.textContent = '等待开始';
                    winnerDisplay.textContent = '';
                    startBtn.disabled = false;
                    resetBtn.disabled = true;
                    break;
                case 1:
                    statusText.textContent = '抢答中...';
                    winnerDisplay.textContent = '';
                    startBtn.disabled = true;
                    resetBtn.disabled = false;
                    break;
                case 2:
                    statusText.textContent = '抢答结束';
                    winnerDisplay.textContent = `抢中者: ${this.winnerId || '未知'}`;
                    startBtn.disabled = true;
                    resetBtn.disabled = false;

                    // 播放音效
                    const sound = document.getElementById('answerSound');
                    if (sound) {
                        sound.currentTime = 0; // 重置音频到开头
                        sound.play().catch(e => console.log('音频播放失败:', e));
                    }

                    break;
            }
        }

        updateConnectionStatus() {
            const statusElement = document.getElementById('connectionStatus');
            if (this.connected) {
                statusElement.textContent = '已连接';
                statusElement.className = 'connection-status connected';
            } else {
                statusElement.textContent = '未连接';
                statusElement.className = 'connection-status disconnected';
            }
        }
    }

    // 初始化客户端
    document.addEventListener('DOMContentLoaded', () => {
        new HostClient();
    });
</script>
</body>
</html>